package org.jpropelleralt.utils.impl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

/**
 * Simple utilities for files
 */
public class FileUtils {

	/**
	 * Read the whole contents of a {@link File} to a {@link String}
	 * @param reader	The {@link File} to read
	 * @return 			The {@link File} contents as a {@link String}
	 * @throws			IOException if {@link File} cannot be read
	 */
	public static String readerToString(Reader reader) throws IOException {
		StringBuffer fileData = new StringBuffer();
		try {
			char[] buf = new char[1024];
			int numRead = 0;
			while ((numRead = reader.read(buf)) != -1) {
				fileData.append(buf, 0, numRead);
			}
			return fileData.toString();
		} finally {
			reader.close();
		}
	}
	
	/**
	 * Read the whole contents of a {@link File} to a {@link String}
	 * @param file		The {@link File} to read
	 * @return 			The {@link File} contents as a {@link String}
	 * @throws			IOException if {@link File} cannot be read
	 */
	public static String fileToString(File file) throws IOException {
		return readerToString(new BufferedReader(new FileReader(file)));
	}
}
